package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.CouponDao;
import com.qc.entity.BossJobEntity;
import com.qc.entity.Coupon;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("couponDao")
public class CouponDaoImpl implements CouponDao{

	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public int saveCoupon(Coupon coupon) {
		
		String sql = "insert into t_coupon(shop_id,title,start_date,end_date,coupon_start_date,coupon_end_date,money,money2,type,num,content,num_number)values(?,?,?,?,?,?,?,?,?,?,?,?)";
		
		return jdbcTemplate.update(sql,new Object[]{coupon.getShopId(),coupon.getTitle(),
								coupon.getStartDate(),coupon.getEndDate(),
								coupon.getCouponStartDate(),coupon.getCouponEndDate(),
								coupon.getMoney(),coupon.getMoney2(),coupon.getType(),
								coupon.getNum(),coupon.getContent(),coupon.getNumNumber()});
	}

	@Override
	public int deleteCouponId(long id) {
		return 0;
	}

	@Override
	public int updateCouponId(Coupon coupon) {
		
		String sql = "update t_coupon set overdue_state = ? ,num_number = ? ,coupon_type = ? ,shelves_state = ? where b_id = ?";
		
		return jdbcTemplate.update(sql,new Object[]{coupon.getOverdueState(),coupon.getNumNumber(),coupon.getCouponType(),coupon.getShelvesState(),coupon.getbId()});
	}

	
	@Override
	public List<Coupon> listAllUserId(long userId,String state) {
		
		String sql = "select a.shop_id,"
				+ "	a.user_id,"
				+ "	a.coupon_id,"
				+ "	a.coupon_number,"
				+ " a.state,"
				+ "	b.title,"
				+ "	b.start_date,"
				+ "	b.end_date,"
				+ "	b.coupon_start_date,"
				+ "	b.coupon_end_date,"
				+ "	b.money,"
				+ "	b.money2,"
				+ "	b.type,"
				+ "	b.num,"
				+ "	b.content "
				+ "	from t_business_receive a "
				+ " left join t_coupon b on a.coupon_id = b.b_id where 1 = 1 and b.overdue_state = 2 and b.coupon_type = 2 and b.shelves_state = 1 ";
			if (userId != 0) {
				sql += " and a.user_id = "+userId+"";
			}
			
			if (StringUtil.isNotEmpty(state)) {
				sql += " and a.state = "+state+"";
			}
		
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BossJobEntity.class));
	}

	@Override
	public List<Coupon> listAllShopId(long userId,long shopId) {
		
		String sql = "select a.*, (SELECT COUNT(*)  FROM t_business_receive WHERE coupon_id = a.b_id and user_id = "+userId+") couponState "
				+ "	from t_coupon a where 1 = 1 and a.shop_id = "+shopId+" and a.overdue_state = 2  and a.coupon_type = 2 and a.shelves_state = 1";
		sql += " order by a.created_time DESC ";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Coupon.class));
	}

	@Override
	public int getListCouponCount(String name, long shopId) {
		
		String sql = "select count(*) from t_coupon where 1 = 1 and shop_id = "+shopId+" ";
		
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<Coupon> getAllCoupon(String name, long shopId, int pageNo, int pageSize) {
		
		int beginRow = (pageNo - 1)* pageSize;
		
		String sql = "select a.* from t_coupon a where 1 = 1 and shop_id = "+shopId+" ";
		
		sql += " order by a.created_time DESC ";
		sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Coupon.class));
	}

	@Override
	public int updateState(long bId, String state) {
		
		String sql = "update t_coupon set shelves_state = "+state+" where b_id = "+bId;
		
		return jdbcTemplate.update(sql);
	}

	@Override
	public List<Coupon> listAllCouponType() {
		
		String sql = "select * from t_coupon where 1 = 1 and coupon_type = 2 ";
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Coupon.class));
	}
	
	@Override
	public List<Coupon> listAllCouponOverdueState() {
		
		String sql = "select * from t_coupon where 1 = 1 and overdue_state = 2 ";
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Coupon.class));
	}

	@Override
	public Coupon getByCouponId(long bId) {
		
		String sql = "select * from t_coupon where b_id = " + bId;

		return (Coupon) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(Coupon.class));
	}

}
